package com.example.aircraftticketingsystemserver.controller;

import com.example.aircraftticketingsystemserver.entity.*;
import com.example.aircraftticketingsystemserver.mapper.FlightsMapper;
import com.example.aircraftticketingsystemserver.service.IFlightsService;
import com.example.aircraftticketingsystemserver.service.INotificationService;
import com.example.aircraftticketingsystemserver.service.IOrdersService;
import com.example.aircraftticketingsystemserver.utils.FlightAndTicket;
import com.example.aircraftticketingsystemserver.utils.Result;
import com.google.gson.Gson;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lxz
 * @since 2024-08-05
 */
@RestController
@CrossOrigin(origins = {"*","null"})
@RequestMapping("/flight")
public class FlightsController {
    @Resource
    private IFlightsService flightsService;
    @Resource
    private IOrdersService ordersService;
    @Resource
    INotificationService notificationService;
    /**
     * 添加航班
     */
    @PostMapping("/addFlight")
    public Result addFlight(@RequestBody Flight flight){
        flightsService.addFlight(flight);
        return Result.ok();
    }
    /**
     * 添加航班 及对应的飞机票
     */
    @PostMapping("/addFlightAndTicket")
    public Result addFlightAndTicket(@RequestBody FlightAndTicket flightAndTicket){
        System.out.println("==============="+flightAndTicket.getFlight());
        System.out.println("++++++++++++++"+flightAndTicket.getTicket());
        flightsService.addTicketByFlightId(flightAndTicket.getFlight(),flightAndTicket.getTicket());
        return Result.ok();
    }
    /**
     *删除单个航班
     */
    @GetMapping("/deleteFlight")
    public Result deleteFlight(Integer flightId){
        flightsService.deleteFlightByFlightId(flightId);
        return Result.ok();
    }
    /**
     * 批量删除航班
     */
    @PostMapping ("/batchDeleteFlight")
    public Result batchDeleteFlight(@RequestBody List<Integer> flightIds){
        flightsService.batchDeleteFlight(flightIds);
        return Result.ok();
    }
    /**
     * 更新航班
     */
    @PostMapping("/updateFlight")
    public Result updateFlight(@RequestBody Flight flight){
        Flight flightById = flightsService.getFlightById(flight.getFlightId());
        String status = flightById.getStatus();
        flightsService.updateFlight(flight);
        if(!status.equals(flight.getStatus())){
        // 查找所有与该航班相关的订单
        List<Order> orders = ordersService.getOrderByFlightId(flight.getFlightId());
        // 为每个相关的订单创建一条通知
        for (Order order : orders) {

            if(order.getStatus().equals("已支付")){
                System.out.println(order.getStatus());
            Notification notification = new Notification();
            notification.setUserId(order.getUserId());
            notification.setFlightId(flight.getFlightId());
            notification.setOrderId(order.getOrderId());
            notification.setMessage("您的订单号:"+order.getOrderId() +"  航班号为："+ flight.getFlightNumber() + "由"+flight.getDepartureCity()+" 前往"+flight.getArrivalCity()+"  的航班状态已更新为：" + flight.getStatus()+"  起飞时间为："+flight.getDepartureTime()+"  落地时间为："+ flight.getArrivalTime());
            notificationService.addNotice(notification);}
        }
        }
        return Result.ok();
    }
    /**
     * 分页搜索查询
     */
    @GetMapping("/getFlightsPage")
    public Result getFlightsPage(@RequestParam(value = "pageNo") Integer pageNo,
                                 @RequestParam(value = "pageSize") Integer pageSize,
                                 String flightNumber,
                                 String airline,
                                 String departureCity,
                                 String arrivalCity,
                                 String departureAirport,
                                 String arrivalAirport,
                                 String status,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date start,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date end){
        return Result.ok(flightsService.getFlightsPage(pageNo, pageSize, flightNumber, airline, departureCity, arrivalCity, departureAirport, arrivalAirport, status,start, end));
    }
    /**
     * 查询航班列表
     */
    @GetMapping("/getFlightList")
    public Result getFlightList( String flightNumber,
                                 String airline,
                                 String departureCity,
                                 String arrivalCity,
                                 String departureAirport,
                                 String arrivalAirport,
                                 String status,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date start,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date end){
        return Result.ok(flightsService.getFLightList(flightNumber, airline, departureCity, arrivalCity, departureAirport, arrivalAirport, status, start, end));
    }
    /**
     * 查询中转航班
     */
    @GetMapping("/getTransferFlight")
    public Result getTransferFlight(@RequestParam("departureCity") String departureCity,@RequestParam("arrivalCity") String arrivalCity){
        List<TransferFlight> transferFlight = flightsService.getTransferFlight(departureCity, arrivalCity);
        if(transferFlight.isEmpty()){
            return Result.error("不存在中转航班");
        }
        else return Result.ok(transferFlight);

    }
}
